<template>
	<basic-container>
		<avue-crud :option="option" :data="data" v-model="form" :page="page" ref="crud" @search-change="searchChange"
		 @search-reset="searchReset" @current-change="currentChange" @size-change="sizeChange" @row-update="rowUpdate"
		 @on-load="onLoad" :before-open="beforeOpen">
		</avue-crud>
	</basic-container>
</template>

<script>
	import request from '@/router/axios';
	export default {
		data() {
			return {
				query: {},
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				form: {},
				option: {
					searchMenuSpan: 4,
					border: true,
					index: true,
					refreshBtn: false,
					addBtn: false,
					delBtn: false,
					//editBtnText:'仓库',
					excelBtn: true,
					menuWidth: '80px',
					column: [{
							label: "登录账号",
							prop: "loginName",
							search: true,
							searchPlaceholder: '帐号&姓名',
							width: "150px",
							display: false
						},
						{
							label: "真实姓名",
							prop: "trueName",
							width: "150px",
							display: false
						},
						{
							label: "手机号码",
							prop: "mobile",
							display: false
						},
						{
							label: "电子邮箱",
							prop: "email",
							display: false
						},
						{
							label: "用户性别",
							prop: "sex",
							type: "radio",
							dicData: [{
									label: "男",
									value: 1
								},
								{
									label: "女",
									value: 2
								},
								{
									label: "未知",
									value: 3
								}
							],
							display: false
						},
						{
							label: "职务",
							prop: "remark",
							type: "select",
							filterable: true,
							dicData: [],
							span: 24,
							props: {
								label: "name",
								value: "code"
							},
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "相关仓库",
							prop: "warehouse",
							type: "cascader",
							hide: true,
							filterable: true,
							multiple: true,
							dicData: [],
							span: 24,
							rules: [{
								required: true,
								trigger: "blur"
							}]
						}
					]
				},
				data: []
			};
		},
		created() {
			request({
				url: '/api/code/getCodes',
				params: {
					typeId: 'CODE200901'
				}
			}).then(res => {
				this.findObject(this.option.column, 'remark').dicData = res.data.data;
			});
			request({
				url: '2/api/BaseOrganization/cascader'
			}).then(res => {
				console.log(res.data)
				this.findObject(this.option.column, 'warehouse').dicData = res.data.data;
			});
		},
		mounted() {},
		methods: {
			beforeOpen(done, type) {
				if (["edit"].includes(type)) {
					this.request({
						url: '2/api/BaseUser/pages',
						params: {
							key: this.form.loginName,
							limit: 0
						}
					}).then(res => {
						const data = res.data.data
						// let p = data.find(m => m.type == 1);
						// if (p != null) {
						// 	this.form.remark = p.code
						// }
						let w = []
						data.filter(m => m.type == 2).forEach(m => {
							let r = []
							r.push(m.org)
							r.push(m.code)
							w.push(r)
						})
						this.form.warehouse = w
					});
				}
				done();
			},
			rowUpdate(row, index, loading, done) {
				let parm = [];
				let v = {};
				v.uid = row.loginName;
				v.code = row.remark;
				v.type = 1;
				parm.push(v);
				row.warehouse.forEach(m => {
					let v = {};
					v.uid = row.loginName;
					v.code = m[1];
					v.type = 2;
					parm.push(v);
				})
				return this.request({
					url: '2/api/BaseUser/edit',
					method: 'post',
					data: parm
				}).then(() => {
					loading();
					this.onLoad(this.page);
					done();
				}, error => {
					console.log(error);
				});
			},
			searchReset() {
				this.query = {};
				this.page.currentPage = 1;
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.page.currentPage = 1;
				this.query = params;
				this.onLoad(this.page);
				done();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			onLoad(page) {
				this.request({
					url: '2/api/BaseUser/userByTenant',
					params: {
						key: this.query.loginName == undefined ? '' : this.query.loginName,
						page: page.currentPage,
						limit: page.pageSize
					}
				}).then(res => {
					const data = res.data
					this.data = data.data;
					this.page.total = data.remark;
				});
			}
		}
	};
</script>

<style>
</style>
